package com.room.search.web.rest;

import com.room.search.repository.RoomUserBrokerChantMessageRepository;
import com.room.search.service.RoomUserBrokerChantMessageService;
import com.room.search.service.dto.RoomUserBrokerChantMessageDTO;
import com.room.search.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.room.search.domain.RoomUserBrokerChantMessage}.
 */
@RestController
@RequestMapping("/api")
public class RoomUserBrokerChantMessageResource {

    private final Logger log = LoggerFactory.getLogger(RoomUserBrokerChantMessageResource.class);

    private static final String ENTITY_NAME = "roomUserBrokerChantMessage";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final RoomUserBrokerChantMessageService roomUserBrokerChantMessageService;

    private final RoomUserBrokerChantMessageRepository roomUserBrokerChantMessageRepository;

    public RoomUserBrokerChantMessageResource(
        RoomUserBrokerChantMessageService roomUserBrokerChantMessageService,
        RoomUserBrokerChantMessageRepository roomUserBrokerChantMessageRepository
    ) {
        this.roomUserBrokerChantMessageService = roomUserBrokerChantMessageService;
        this.roomUserBrokerChantMessageRepository = roomUserBrokerChantMessageRepository;
    }

    /**
     * {@code POST  /room-user-broker-chant-messages} : Create a new roomUserBrokerChantMessage.
     *
     * @param roomUserBrokerChantMessageDTO the roomUserBrokerChantMessageDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new roomUserBrokerChantMessageDTO, or with status {@code 400 (Bad Request)} if the roomUserBrokerChantMessage has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/room-user-broker-chant-messages")
    public ResponseEntity<RoomUserBrokerChantMessageDTO> createRoomUserBrokerChantMessage(
        @Valid @RequestBody RoomUserBrokerChantMessageDTO roomUserBrokerChantMessageDTO
    ) throws URISyntaxException {
        log.debug("REST request to save RoomUserBrokerChantMessage : {}", roomUserBrokerChantMessageDTO);
        if (roomUserBrokerChantMessageDTO.getId() != null) {
            throw new BadRequestAlertException("A new roomUserBrokerChantMessage cannot already have an ID", ENTITY_NAME, "idexists");
        }
        RoomUserBrokerChantMessageDTO result = roomUserBrokerChantMessageService.save(roomUserBrokerChantMessageDTO);
        return ResponseEntity
            .created(new URI("/api/room-user-broker-chant-messages/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /room-user-broker-chant-messages/:id} : Updates an existing roomUserBrokerChantMessage.
     *
     * @param id the id of the roomUserBrokerChantMessageDTO to save.
     * @param roomUserBrokerChantMessageDTO the roomUserBrokerChantMessageDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserBrokerChantMessageDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserBrokerChantMessageDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the roomUserBrokerChantMessageDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/room-user-broker-chant-messages/{id}")
    public ResponseEntity<RoomUserBrokerChantMessageDTO> updateRoomUserBrokerChantMessage(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody RoomUserBrokerChantMessageDTO roomUserBrokerChantMessageDTO
    ) throws URISyntaxException {
        log.debug("REST request to update RoomUserBrokerChantMessage : {}, {}", id, roomUserBrokerChantMessageDTO);
        if (roomUserBrokerChantMessageDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserBrokerChantMessageDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserBrokerChantMessageRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        RoomUserBrokerChantMessageDTO result = roomUserBrokerChantMessageService.save(roomUserBrokerChantMessageDTO);
        return ResponseEntity
            .ok()
            .headers(
                HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserBrokerChantMessageDTO.getId().toString())
            )
            .body(result);
    }

    /**
     * {@code PATCH  /room-user-broker-chant-messages/:id} : Partial updates given fields of an existing roomUserBrokerChantMessage, field will ignore if it is null
     *
     * @param id the id of the roomUserBrokerChantMessageDTO to save.
     * @param roomUserBrokerChantMessageDTO the roomUserBrokerChantMessageDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserBrokerChantMessageDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserBrokerChantMessageDTO is not valid,
     * or with status {@code 404 (Not Found)} if the roomUserBrokerChantMessageDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the roomUserBrokerChantMessageDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/room-user-broker-chant-messages/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<RoomUserBrokerChantMessageDTO> partialUpdateRoomUserBrokerChantMessage(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody RoomUserBrokerChantMessageDTO roomUserBrokerChantMessageDTO
    ) throws URISyntaxException {
        log.debug("REST request to partial update RoomUserBrokerChantMessage partially : {}, {}", id, roomUserBrokerChantMessageDTO);
        if (roomUserBrokerChantMessageDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserBrokerChantMessageDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserBrokerChantMessageRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<RoomUserBrokerChantMessageDTO> result = roomUserBrokerChantMessageService.partialUpdate(roomUserBrokerChantMessageDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserBrokerChantMessageDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /room-user-broker-chant-messages} : get all the roomUserBrokerChantMessages.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of roomUserBrokerChantMessages in body.
     */
    @GetMapping("/room-user-broker-chant-messages")
    public List<RoomUserBrokerChantMessageDTO> getAllRoomUserBrokerChantMessages() {
        log.debug("REST request to get all RoomUserBrokerChantMessages");
        return roomUserBrokerChantMessageService.findAll();
    }

    /**
     * {@code GET  /room-user-broker-chant-messages/:id} : get the "id" roomUserBrokerChantMessage.
     *
     * @param id the id of the roomUserBrokerChantMessageDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the roomUserBrokerChantMessageDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/room-user-broker-chant-messages/{id}")
    public ResponseEntity<RoomUserBrokerChantMessageDTO> getRoomUserBrokerChantMessage(@PathVariable Long id) {
        log.debug("REST request to get RoomUserBrokerChantMessage : {}", id);
        Optional<RoomUserBrokerChantMessageDTO> roomUserBrokerChantMessageDTO = roomUserBrokerChantMessageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(roomUserBrokerChantMessageDTO);
    }

    /**
     * {@code DELETE  /room-user-broker-chant-messages/:id} : delete the "id" roomUserBrokerChantMessage.
     *
     * @param id the id of the roomUserBrokerChantMessageDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/room-user-broker-chant-messages/{id}")
    public ResponseEntity<Void> deleteRoomUserBrokerChantMessage(@PathVariable Long id) {
        log.debug("REST request to delete RoomUserBrokerChantMessage : {}", id);
        roomUserBrokerChantMessageService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
            .build();
    }
}
